const passport = require('koa-passport')
const passportLocal = require('passport-local')
const userModel = require('../../dbs/models/user')

passport.use(new passportLocal(async (username, password, done) => {
    let where = { username }
    let result = await userModel.findOne(where) //通过username查询密码
    // global.console.log(username, password, done,result)
    if (result != null) {
        if (result.password == password) {
            return done(null, result)
        } else {
            done(null, false, 'password wrong...')
        }
    } else {
        return done(null, false, 'user not exist...')
    }
}))
// 序列化，查到用户，存入session
passport.serializeUser((user, done) => {
    done(null, user)
})
// 反序列化，每次请求会从session读取用户
passport.deserializeUser((user, done) => {
    return done(null, user)
})

module.exports = passport